package com.quartz.quartz;

import com.quartz.entity.QuartzObj;
import org.quartz.*;
import org.quartz.impl.matchers.GroupMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: lixiao
 * Created Date: 2020/7/31 17:50
 */
@Service(value = "quartzUtil")
public class QuartzUtil {


    @Autowired
    Scheduler getScheduler;

    public  void add(QuartzObj quartzObj) throws Exception {
        getScheduler.start();
        Job job =(Job) Class.forName(quartzObj.getClassName()).newInstance();
        //检查是否更新
        TriggerKey triggerKey = TriggerKey.triggerKey(quartzObj.getClassName(), quartzObj.getPackageName());
        //查询是否存在trigger
        CronTrigger cronTrigger= (CronTrigger) getScheduler.getTrigger(triggerKey);
        //判断是否时新增
        if(cronTrigger==null){
            //新建job
            JobDetail newJob = JobBuilder.newJob(job.getClass()).withIdentity(quartzObj.getClassName(), quartzObj.getPackageName()).build();
            //创建cron.1、加载cron表达式  2、创建trigger
            CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule(quartzObj.getCronExp());
            cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(cron).build();
            getScheduler.scheduleJob(newJob,cronTrigger);
        }else {
            //创建cron.1、加载cron表达式  2、创建trigger
            CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule(quartzObj.getCronExp());
            cronTrigger = TriggerBuilder.newTrigger().withIdentity(triggerKey).withSchedule(cron).build();
            getScheduler.rescheduleJob(triggerKey,cronTrigger);

        }
    }
    public  void del(QuartzObj quartzObj) throws SchedulerException {
        TriggerKey triggerKey=TriggerKey.triggerKey(quartzObj.getClassName(),quartzObj.getPackageName());
        //暂停
        getScheduler.pauseTrigger(triggerKey);
        getScheduler.unscheduleJob(triggerKey);
        JobKey jobkey = JobKey.jobKey(quartzObj.getClassName(), quartzObj.getPackageName());
        if(getScheduler.checkExists(triggerKey)){
            getScheduler.deleteJob(jobkey);
        }
    }

    public  void stop(QuartzObj quartzObj) throws SchedulerException {
        JobKey jobkey = JobKey.jobKey(quartzObj.getClassName(), quartzObj.getPackageName());
        getScheduler.pauseJob(jobkey);
    }

    /**
     * 获取当前正在运行的trigger
     * @return
     * @throws SchedulerException
     */
    public List<QuartzObj> getAll() throws SchedulerException {
        List<QuartzObj> list=new ArrayList<>();

        Set<JobKey> jobkeys = getScheduler.getJobKeys(GroupMatcher.anyJobGroup());
        for(JobKey jobKey:jobkeys){
            List<? extends Trigger> triggers = getScheduler.getTriggersOfJob(jobKey);
            //名称
            jobKey.getName();
            for(Trigger trigger:triggers){
                //获取当前任务状态
                Trigger.TriggerState State=getScheduler.getTriggerState(trigger.getKey());
                State.name();
            }
        }
        return  list;

    }
}